package com.example.onlineeducation.ui.mine

import android.content.Context
import android.content.Intent
import android.view.View
import com.example.onlineeducation.App
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.constant.ExamConstants
import com.example.onlineeducation.data.constant.TopicType
import com.example.onlineeducation.data.entity.ExamEntity
import com.example.onlineeducation.databinding.CommonListActivityBinding
import com.example.onlineeducation.databinding.TopicChildItemBinding
import com.example.onlineeducation.ui.online.OnlineExaminationAc
import com.example.onlineeducation.viewmodel.UnpassExamListVM
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.extension.ycSetTextColorRes
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.recycleView.YcRecyclerViewAdapter
import com.yc.jetpacklib.refresh.YcRefreshSpecialUtil
import com.yc.jetpacklib.release.YcSpecialViewSmart

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-05-06
 * UseDes:待通过的考试
 */
class UnpassExamListActivity : BaseActivity<CommonListActivityBinding>(CommonListActivityBinding::inflate) {
    private val mViewModel: UnpassExamListVM by ycViewModels()
    private lateinit var mRefreshUtil: YcRefreshSpecialUtil
    private var mProId: String? = null

    companion object {
        fun newInstance(context: Context) {
            val intent = Intent(context, UnpassExamListActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val mExamAdapter by YcRecyclerViewAdapter.ycLazyInitApply<ExamEntity, TopicChildItemBinding>(
        TopicChildItemBinding::inflate
    ) {
        mOnUpdate = {
            ivStudy.ycLoadImageNet(it.examImage, errorImg = R.drawable.ic_topic)
            tvStudyName.text = it.examName
            tvTotalGrade.visibility = View.INVISIBLE
            tvStudyTime.text = it.examNeedPoint
            tvState.text = ExamConstants.showResultText(it.examPass)
            tvState.ycSetTextColorRes(ExamConstants.showResultTextColor(it.examPass))
            ivState.setImageResource(ExamConstants.showResultIcon(it.examPass))
        }
        mItemClick = { mExamEntity ->
            if (mExamEntity.isExceedTime) {
                mViewModel.getCheckStudyTime(mProId!!, mExamEntity.topicId)
            } else {
                mDialog.setMsg("考试已结束").showSingle()
            }
            mViewModel.mCheckStudyTimeDataModel.observe {
                it.doSuccess {
                    OnlineExaminationAc.newInstance(
                        this@UnpassExamListActivity, mExamEntity.id, mExamEntity.examName, mExamEntity.time, mExamEntity.timeEnd
                    )
                }.ycShowNetError()
            }

        }
    }


    override fun CommonListActivityBinding.initView() {
        actionbarLayout.setTitle(titleName = "待通过考试")
        rvList.adapter = mExamAdapter
        mRefreshUtil = YcRefreshSpecialUtil(this@UnpassExamListActivity, srlList, mExamAdapter, YcSpecialViewSmart(rvList, flRefresh), true)
        srlList.setNoMoreData(true)
        mRefreshUtil.mRefreshAndLoadMore = { isRefresh: Boolean, pageIndex: Int, pageSize: Int ->
            if (isRefresh) {
                getData()
            }
        }
        mViewModel.mExamListDataModel.observe {
            it.doSuccess { data ->
                mExamAdapter.addAllData(data)
            }.ycShowNetError()
            mRefreshUtil.ycRefreshAndLoadMoreResult(it)
        }

        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null && mProId != it.proId) {
                mProId = it.proId
                mRefreshUtil.startRefresh()
            }
        }
    }

    private fun getData() {
        mViewModel.getExamList(mProId!!, TopicType.getTemplateTypeName(TopicType.Type.EXAM, TopicType.Status.INCOMPLETE))
    }
}